/**
 * 全员营销活动|邀请记录 wangzz
 */


var fansPageIndex = 1;
var memPageIndex = 1;
var fansLoadValid = true;
var memLoadValid = true;
var type = 1;

var inviteRecord = {
    init : function(cid,staffId,pageIndex){
        inviteRecord.cid = cid;
        inviteRecord.staffId = staffId;
        inviteRecord.pageIndex = pageIndex;
        return inviteRecord;
    },
    //邀请粉丝记录
    inviteFans : function(pageIndex) {
        var url = inviteRecord.getPath()+"/aem/staffPropertyInfo/ajaxFansPageList";

        $.post(url, {
            "cid":inviteRecord.cid,
            "staffId":inviteRecord.staffId,
            "index":pageIndex,
            "size":10
        }, function (data) {
            if (data.status == "200") {
                var fansList = data.data.list;
                var fansHtml="";
                for(var i = 0;i<fansList.length;i++){
                    fansHtml += '<li>';
                    fansHtml += '<img src="'+fansList[i].photo+'" class="head"/>';
                    fansHtml += '<div class="name">'+fansList[i].nickName+'</div>';
                    fansHtml += '<div class="time">'+formatDateTimeYMDHMSDAY(fansList[i].focusTime)+'</div>';
                    fansHtml += '</li>';
                }
                /*fansHtml += '<div id="next-page" onclick="inviteRecord.nextPage()" class="more" style="margin-bottom:40px">查看更多</div>';*/

                if( pageIndex == 1){
                    $('#fans-list').html(fansHtml);
                }else{
                    $('#fans-list').append(fansHtml);
                }

            }else if(data.status == "300"){
                if(pageIndex == 1) {
                    $("#fans-list").html('<div class="more" style="margin-bottom:40px;margin-top: 100px">暂无数据</div>');
                }
                fansLoadValid = false ;
            }else{
                if(pageIndex == 1) {
                    $("#fans-list").html('<div class="more" style="margin-bottom:40px;margin-top: 100px">暂无数据</div>');
                }
            }
        })
    },
    //邀请会员记录
    inviteMem : function(pageIndex) {
        var url = inviteRecord.getPath()+"/aem/staffPropertyInfo/ajaxMemberPageList";
        $.post(url, {
            "cid":inviteRecord.cid,
            "staffId":inviteRecord.staffId,
            "index":pageIndex,
            "size":10
        }, function (data) {

            if (data.status == "200" ) {

                var memList = data.data.list;
                var memHtml="";
                for(var i = 0;i<memList.length;i++){
                    memHtml += '<li>';
                    memHtml += '<img src="'+memList[i].photo+'" class="head"/>';
                    memHtml += '<div class="name">'+memList[i].name+'</div>';
                    memHtml += '<div class="time">'+formatDateTimeYMDHMSDAY(memList[i].registTime)+'</div>';
                    memHtml += '</li>';
                }
                if( pageIndex == 1){
                    $('#mem-list').html(memHtml);
                }else{
                    $('#mem-list').append(memHtml);
                }
            }else if(data.status == "300"){
                if(pageIndex == 1){
                    $("#mem-list").html('<div class="more" style="margin-bottom:40px;margin-top: 100px">暂无数据</div>');
                }
                memLoadValid = false ;
            }else{
                if(pageIndex == 1) {
                    $("#fans-list").html('<div class="more" style="margin-bottom:40px;margin-top: 100px">暂无数据</div>');
                }
            }
        })
    },

    /*nextPage : function(){
        fansPageIndex++;
        inviteRecord.inviteFans(fansPageIndex);
    },*/



    getPath : function(){
        var parse = inviteRecord.parseUrl(window.location.href);
        var path = parse.protocol + '://' + parse.host + ':' + parse.port;
        if("scrm_sps" == parse.segments[0]){
            path +=  '/' + parse.segments[0];
        }
        return path;
    },

    parseUrl : function(url) {
        var a =  document.createElement('a');
        a.href = url;
        return {
            source: url,
            protocol: a.protocol.replace(':',''),
            host: a.hostname,
            port: a.port,
            query: a.search,
            params: (function(){
                var ret = {},
                    seg = a.search.replace(/^\?/,'').split('&'),
                    len = seg.length, i = 0, s;
                for (;i<len;i++) {
                    if (!seg[i]) { continue; }
                    s = seg[i].split('=');
                    ret[s[0]] = s[1];
                }
                return ret;
            })(),
            file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
            hash: a.hash.replace('#',''),
            path: a.pathname.replace(/^([^\/])/,'/$1'),
            relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
            segments: a.pathname.replace(/^\//,'').split('/')
        };
    }
}


$('#fans').click(function(){
    fansLoadValid = true;

    $('.mint-tab-item').removeClass("is-selected");
    $('#fans').addClass("is-selected");
    $("#fans_show").attr("style","display:block;");
    $("#mem_show").attr("style","display:none;");
    type=1;
});

$('#member').click(function(){
    memLoadValid = true;

    $('.mint-tab-item').removeClass("is-selected");
    $('#member').addClass("is-selected");
    $("#fans_show").attr("style","display:none;");
    $("#mem_show").attr("style","display:block;");
    type=2;
});

//监听是否滑到底部
window.addEventListener('scroll', throttle(loadData, 500, {leading:false}), false);
//加载更多数据
function loadData(){
    if(type==1){
        if($(window).scrollTop() + $(window).height() >= $(document).height()&&fansLoadValid) {
            fansPageIndex++;
            inviteRecord.inviteFans(fansPageIndex);
        }
    }else{
        if($(window).scrollTop() + $(window).height() >= $(document).height()&&memLoadValid) {
            memPageIndex++;
            inviteRecord.inviteMem(memPageIndex);
        }
    }
}
//频率控制函数
function throttle(fn,delay,options) {
    var wait=false;
    if (!options) options = {};
    return function(){
        var that = this,args=arguments;
        if(!wait){
            if (!(options.leading === false)){
                fn.apply(that,args);
            }
            wait=true;
            setTimeout(function () {
                if (!(options.trailing === false)){
                    fn.apply(that,args);
                }
                wait=false;
            },delay);
        }
    }
}